package com.qf.base.utils;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
//上下文  获取getbean  跟spring容器绑定  根据上下文来获取想要的bean
@Component
public class SpringContextUtil implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) {
        SpringContextUtil.applicationContext=applicationContext;
    }

    /**
     * 根据bean的名称获取springbean
     * @param beanName  bean的名称
     * @param <T>   泛型
     * @return  返回这个bean
     */
    public static <T> T getBean(String beanName){
        return (T) applicationContext.getBean(beanName);
    }

    /**
     * 根据class类型获取 springbean
     * @param clazz bean的类型
     * @param <T>   泛型
     * @return  返回这个bean
     */
    public static <T> T getBean(Class<T> clazz){return applicationContext.getBean(clazz);}
}
